//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    color:'white',
    active:'玄幻奇幻',
    scrollHeight:'',
    motto: 'Hello World',
    userInfo: {},
    SearchData: {
      value:'',
      requestPage:1
    },
    records: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    searchLoading: true,  //把"上拉加载"的变量设为true，显示  
    searchLoadingComplete: false, //把“没有数据”设为false，隐藏
    searchLoadingCompleteNo: false, 
  },
  //事件处理函数
  SearchInput:function(e){
   this.setData({
     'SearchData.value': e.detail.value
   });
  },
  SearchClear:function(){
    this.setData({
      'SearchData.value':''
    });
  },

  //右上角分享功能
  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '',
      path: '/pages/list/list?id=' + that.data.scratchId,
      success: function (res) {
        // 转发成功

        that.shareClick();
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  SearchConfirm:function(){
    var that=this;
    var pageSize='';
    if (that.data.SearchData.value!=''){
      pageSize='9999'
      that.setData({
        searchLoadingComplete: false,
        searchLoading: false,
        searchLoadingCompleteNo: false 
      })
    }else{
      pageSize = '12'
      that.setData({
        searchLoading: true,  //把"上拉加载"的变量设为true，显示  
        searchLoadingComplete: false, //把“没有数据”设为false，隐藏
        searchLoadingCompleteNo: false 
      })
    }
    wx.request({
      url: app.globalData.serverIp +'book/getBookList.json', 
      data: {
        'searchCondition': that.data.SearchData.value,
        'requestPage': '1',
        'pageSize': pageSize,
        'type': that.data.active 
      },
      
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data.data.sendData.pager.recordCount)
        that.setData({
          records: res.data.data.sendData.records,
        })
        if (res.data.data.sendData.pager.recordCount==0){
          that.setData({
            searchLoadingCompleteNo: true
          })
        } 
      }
    })    
  },
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
   
    /*设置默认标题 */
    wx.setNavigationBarTitle({
      title: 'Ybook'
    })
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {//授权了，可以获取用户信息了
          wx.getUserInfo({
            success: (res) => {
              console.log("已授权")
            }
          })
        } else {//未授权，跳到授权页面
          wx.redirectTo({
            url: '../grant/grant',//授权页面
          })
        }
      }
    })
    //获取视图高度
    var scrollHeight = wx.getSystemInfoSync().windowHeight;
    this.setData({
      scrollHeight: scrollHeight-100
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        console.log(res.userInfo.nickName)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
     
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({ 
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
      console.log(3)

    }
    var that = this; //把page对象赋值给新建的对象
    wx.request({
      url: app.globalData.serverIp + 'book/getBookList.json',
      data: {
        'requestPage':'1',
        'pageSize': '12',
        'type': that.data.active 
      },
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          records: res.data.data.sendData.records
        })
      }
    })
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  /*title点击变色*/
  click: function (event){
    var that=this;
    var text = event.currentTarget.dataset.keyword;
    that.setData({
      active: text
    })
    wx.request({
      url: app.globalData.serverIp + 'book/getBookList.json',
      data: {
        'requestPage': '1',
        'pageSize': '12',
        'type': that.data.active
      },
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          records: res.data.data.sendData.records
        })
      }
    })
  },
  go: function (event){
    var id = event.currentTarget.dataset.keyword;
    var bookName = event.currentTarget.dataset.name; 
    wx.navigateTo({
      url: '../item/item?id='+id,
    })
    
  },
  //滚动到底部触发事件  
     searchScrollLower: function() {
         var that = this;
         if(that.data.searchLoading && !that.data.searchLoadingComplete) {
             that.setData({
               'SearchData.requestPage': that.data.SearchData.requestPage + 1,  //每次触发拉事件，把requestPage+1  
       });
             wx.request({
               url: app.globalData.serverIp + 'book/getBookList.json',
               data: {
                 'requestPage': that.data.SearchData.requestPage,
                 'pageSize':'12',
                 'type':that.data.active 
               },
               method: "POST",
               header: {
                 'content-type': 'application/json' // 默认值
               },
               success(res) {
                 that.setData({
                   records: that.data.records.concat(res.data.data.sendData.records)
                 })
                 if (res.data.data.sendData.pager.pageCount == 0){
                   that.setData({
                     searchLoadingComplete: true,
                     searchLoading:false,
                     searchLoadingCompleteNo: false 
                   })
                 } else {
                   that.setData({
                     searchLoadingComplete: false,
                     searchLoading: true,
                      searchLoadingCompleteNo: false 
                   })
                 }  
               }
             })
    }
    
  }  
})
